﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;

namespace CPPEI.ETL.WebApi.DtSer.ExtDS
{
    [CPPEI.WebApi.AuthPostAction("ExtDS", "CreateDataService", "1.0")]
    public class CreateDataServiceAction : BaseAction<CreateDataServiceParams, CreateDataServiceResult>
    {
        protected override ActionResult<CreateDataServiceResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            CreateDataServiceParams opParams)
        {

            Guid categoryId = new Guid("{D862B06A-6045-4162-AC5C-9F144039711F}");
            Guid typeId = new Guid("{C7E6E4CA-FC41-41E7-A693-201607A27B39}");

            string address = string.Format("{0};{1}", opParams.Assembly, opParams.Type);

            SER.DtSerService dtSerService = new SER.DtSerService();
            ServiceResult<ENT.DtSer> serResult = dtSerService.CreateDtSer(context.UserId, opParams.FolderId,
                opParams.Name,
               address,
                false,
                string.Empty,
                string.Empty,
                categoryId,
                typeId,
                opParams.Description);
            if (serResult.IsSuccess)
            {
                CreateDataServiceResult result = new CreateDataServiceResult();
                result.Id = serResult.Data.Id;
                return new ActionResult<CreateDataServiceResult>(result);
            }
            else
            {
                return new ActionResult<CreateDataServiceResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class CreateDataServiceParams
    {
        public string Name { get; set; }

        /// <summary>
        /// 外部扩展服务的程序集
        /// </summary>
        public string Assembly { get; set; }

        /// <summary>
        /// 外部扩展服务类型
        /// </summary>
        public string Type { get; set; }

        public string Description { get; set; }

        public Guid FolderId { get; set; }
    }

    public class CreateDataServiceResult
    {
        public Guid Id { get; set; }
    }
}
